package com.comdosoft.comdoandroidbase.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.comdosoft.comdoandroidbase.MyApplication;
import com.comdosoft.comdoandroidbase.R;
import com.comdosoft.comdoandroidbase.adapter.SelectAdapter;
import com.comdosoft.comdoandroidbase.util.NewToast;

import java.util.ArrayList;
import java.util.List;

/**
 * 常用Dialog合集
 * 1、带编辑框的dialog
 * 2、带动画的popupwindow
 *
 * @author niemin
 */
public class DialogFragment extends Fragment implements View.OnClickListener {
    private View view;
    private LayoutInflater inflaters;
    private WindowManager.LayoutParams lp;

    private Button dialog_EditText;
    private Button popupwindow;

    private PopupWindow popu;
    private List<String> datalist = new ArrayList<String>();
    private SelectAdapter<String> adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_dialog, container, false);
        initView();

        inflaters = LayoutInflater.from(getActivity());
        lp = getActivity().getWindow().getAttributes(); // 设置popupWindow弹出后背景的阴影

        return view;
    }

    private void initView() {
        dialog_EditText = (Button) view.findViewById(R.id.dialog_EditText);
        popupwindow = (Button) view.findViewById(R.id.popupwindow);
        popupwindow.setOnClickListener(this);
        dialog_EditText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Intent intent;
        switch (v.getId()) {
            case R.id.dialog_EditText:
			/*
			 * 带编辑框的dialog
			 */
                editTextDialog();
                break;
            case R.id.popupwindow:
			/*
			 * 带动画的popupwindow
			 */
                choosePopu();
                break;
            default:
                break;
        }

    }

    /*
	 * 带编辑框的dialog
	 */
    private void editTextDialog() {
        final Dialog dialog = new Dialog(getActivity(), R.style.MyDialog);
        dialog.setContentView(R.layout.dialog_edit_stock);
        dialog.setCancelable(false);
        dialog.show();

        Button dialog_button_ok = (Button) dialog.findViewById(R.id.dialog_button_ok);
        Button dialog_button_cancel = (Button) dialog.findViewById(R.id.dialog_button_cancel);
        final EditText dialog_text = (EditText) dialog.findViewById(R.id.dialog_text);

        dialog.setTitle("编辑内容");
        dialog_button_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                MyApplication.hideSoftKeyboard(getActivity());
            }
        });
        dialog_button_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (!dialog_text.getText().toString().equals("")) {
                    dialog_EditText.setText(dialog_text.getText().toString());
                    dialog.dismiss();
                    MyApplication.hideSoftKeyboard(getActivity());
                } else {
                    NewToast.makeText(getActivity(), "请输入内容");
                }
            }
        });

    }

    /*
     * 带动画的popupwindow
     */
    private void choosePopu() {
        datalist.clear();
        for (int i = 0; i < 10; i++) {
            datalist.add("测试" + i);
        }

        View selectView = inflaters.inflate(R.layout.pop_selector, null);
        ListView listView = (ListView) selectView.findViewById(R.id.list_one);
        int height = datalist.size();
        if (datalist.size() > 5) {
            height = 5;
            LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) listView.getLayoutParams();
            params.height = popupwindow.getHeight() * height;
            listView.setLayoutParams(params);
        }
        adapter = new SelectAdapter<String>(getActivity(), datalist);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                popupwindow.setText(datalist.get(position));
                adapter.notifyDataSetChanged();
                popu.dismiss();
            }
        });

        popu = new PopupWindow(selectView);
        popu.setWidth(popupwindow.getWidth());
        popu.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popu.setOutsideTouchable(true);
        popu.setFocusable(true);
        lp.alpha = 0.4f;
        getActivity().getWindow().setAttributes(lp);
        popu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                lp.alpha = 1.0f;
                getActivity().getWindow().setAttributes(lp);
            }
        });
        popu.setBackgroundDrawable(new ColorDrawable());
        int[] location = new int[2];
        popupwindow.getLocationOnScreen(location);

        //在控件下方，popup 从上向下显示
        popu.setAnimationStyle(R.style.TypeSelAnimationFade);
        popu.showAsDropDown(popupwindow);

        //在空间上方，popup 从下向上显示
        //		popu.setAnimationStyle(R.style.TypeSelAnimationFadeBottom);
        //		popu.showAtLocation(popupwindow, Gravity.NO_GRAVITY,
        //				location[0], location[1]-popupwindow.getHeight()*height);

    }
}

